package com.ljl.ningwei.controller;


import com.ljl.ningwei.common.result.Result;
import com.ljl.ningwei.service.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
@Slf4j
public class FileController {


    private final FileService fileService;
    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传: {}", file);
        String fullPath;
        try {
            fullPath = fileService.upload(file);
            log.info("文件上传成功,路径为: {}", fullPath);
        } catch (IOException e) {
            log.error("文件上传失败", e);
            throw new RuntimeException(e);
        }
        return Result.success(fullPath);
    }
}
